package cn.edu.sjtu.se.dslab.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import cn.edu.sjtu.se.dslab.actionbean.PageBean;
import cn.edu.sjtu.se.dslab.actionbean.UserActionBean;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.pojo.User;
import cn.edu.sjtu.se.dslab.service.IUserService;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.Message;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 系统用户管理Action处理类
 * @author 孙科
 */
public class UserAction extends ActionSupport implements ModelDriven<UserActionBean>{
	
	private UserAction(){}
	
	/**
	 * 定义日志处理对象
	 */
	private static final Log log = LogFactory.getLog(UserAction.class);
	

	/**
	 * 版本ID
	 */
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * 定义页面信息集合
	 */
	
	private UserActionBean userActionBean = new UserActionBean();
	
	
	/**
	 * 	页面提交信息取得<BR>
	 * 	@return	页面提交信息
	 */
	public UserActionBean getModel()
	{
		return userActionBean;
	}
	private IUserService<UserActionBean>  service;
	
	
	public UserActionBean getUserActionBean() {
		return userActionBean;
	}


	public void setUserActionBean(UserActionBean userActionBean) {
		this.userActionBean = userActionBean;
	}


	public IUserService<UserActionBean> getService() {
		return service;
	}


	public void setService(IUserService<UserActionBean> service) {
		this.service = service;
	}

	public String ModifyView()
	{
		return Const.MODIFY_VIEW;
	}
	
	public String DeleteView()
	{
		System.out.println("id:"+userActionBean.getTxtUserId());
		service.delete(new User(userActionBean.getTxtUserId()));
		try {
			init();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return Const.DELETE_VIEW;
	}
	/**
	 * �鿴������ϸ��Ϣ<BR>
	 * 
	 * @return �鿴����־λ
	 * @exception
	 */
	
	@SuppressWarnings("unchecked")
	public String retrieve() throws Exception {
		// log���
		log.info(Message.USER_RETRIEVE);
		//获取request
		Map request = (Map) ActionContext.getContext().get(Const.REQUEST);
		//获取request
		HttpServletRequest httpRequest = ServletActionContext.getRequest();
		//获取session
		HttpSession session = httpRequest.getSession();
		try {
			User user = (User)session.getAttribute(Const.SESSIONNAME);

			request.put(Const.USER, user);

			this.addActionMessage(Message.USER_RETRIEVE_SUCCESS);


			log.info(Message.USER_RETRIEVE_SUCCESS);
			return Const.RETRIEVE_SUCCESS;
		} catch (Exception e) {
			this.addActionError(Message.USER_RETRIEVE_FAIL);
			this.addFieldError("message", Message.USER_RETRIEVE_FAIL);
			//添加log
			log.error(e.getMessage());
			return Const.RETRIEVE_FAIL;
		}
	}
	@SuppressWarnings("unchecked")
	public String init() throws Exception {

		//获取request
		Map request = (Map) ActionContext.getContext().get(Const.REQUEST);

		try
		{
			//查询结果
			PageBean bean = service.getUserList(userActionBean);

			//将查询结果list放入request中
			request.put(Const.BEAN, bean);


			//返回查询成功标志
			return Const.SELECT;
		}
		catch(DBException e)
		{
			//添加错误信息至页面
			this.addActionError(e.getMessage());
			this.addFieldError("message", e.getMessage());
			//添加log
			log.error(e.getMessage());

			//返回错误标志
			return Const.ERROR;
		}
	}
	
}
